from datetime import datetime,timedelta
from sql import sqlseverDB
from mini_tools.mytools import update_config
from config import SQL_SERVER,SQL_USER,SQL_PWD
import os,json

def get_qukong_data():

    ttms=sqlseverDB('{SQL Server}', SQL_SERVER, 'SuMaiTongPol', SQL_USER, SQL_PWD)
    rs=ttms.ExecQuerydict('select * from Task where gtname is not null and position!=-1 order by gtname,position')
    task_dict={}
    for r in rs:
        r.pop('tcount')
        r.pop('state')
        r.pop('logPath')
        r.pop('LastRunTime')
        r.pop('LastEndTime')
        r.pop('LastHouTaiID')
        r.pop('ShopClass')
        r.pop('FuncName')
        #r.pop('extraParam')
        gtname=r['gtName']
        if task_dict.get(gtname):
            task_dict[gtname].append(r)
        else:
            task_dict[gtname]=[r]

    update_config({'QKTask':task_dict})

def update_pod_daochu(params):

    ttms=sqlseverDB('{SQL Server}', SQL_SERVER, 'SuMaiTongPol', SQL_USER, SQL_PWD)

    aff=ttms.ExecNoQuery('''MERGE INTO YNPodPrice AS target
                USING (SELECT ? as nowTime, ? AS price, ? AS ph_itemid, ? AS daochu,? as fac_name) AS source
                ON target.price = source.price AND target.ph_itemid = source.ph_itemid                                   
                WHEN MATCHED THEN 
                    UPDATE SET daochu = source.daochu,update_time=source.nowTime,fac_name=source.fac_name
                WHEN NOT MATCHED THEN 
                    INSERT (upload_time,price, ph_itemid, daochu,fac_name) 
                    VALUES (source.nowTime,source.price, source.ph_itemid, source.daochu,source.fac_name);''',
                        params)
    
    return aff,params[2]

    
    
